using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;

namespace CustomCMS.API.ObjectModel
{
    public class ElementInfo
    {
        public string Author;
		public string Title;
        public DateTime Date;
        public int Revision;
        public string Comment;
		public string Template;
		public string LockedBy;
        public string Id;
        public string SectionStates;

        public ElementInfo() { }

        public ElementInfo(string id)
        {
            Id = id;
        }

		public ElementInfo(string author, string title, DateTime date, int revision, string comment, string template, string lockedBy, string sectionStates)
		{
			Author = author;
			Title = title;
			Date = date;
			Revision = revision;
			Comment = comment;
			Template = template;
			LockedBy = lockedBy;
            SectionStates = sectionStates;
		}

        public bool Equal(ElementInfo b)
        {
            return Author == b.Author && Title == b.Title && Date == b.Date && Revision == b.Revision && Comment == b.Comment && Template == b.Template && LockedBy == b.LockedBy && Id == b.Id;
        }

        public static int LaterThan(ElementInfo a, ElementInfo b)
        {
            return a.Date > b.Date ? 1 : a.Date < b.Date ? -1 : a.Revision > b.Revision ? 1 : a.Revision < b.Revision ? -1 : 0;
        }

        public string ToXml()
        {
            StringBuilder xml = new StringBuilder("<version");
            xml.Append(ToXml("Id", Id));
            xml.Append(ToXml("Revision", Revision.ToString()));
            xml.Append(ToXml("Date", Date.ToString()));
            xml.Append(ToXml("Author", Author));
            xml.Append(ToXml("Title", Title));
            xml.Append(ToXml("Comment", Comment));
            xml.Append(ToXml("Template", Template));
            xml.Append(ToXml("LockedBy", LockedBy));
            xml.Append("/>");
            return xml.ToString();
        }

        public static string ToXml(ElementInfo info)
        {
            return info.ToXml();
        }

        private static string ToXml(string name, string value)
        {
            return " " + name + "=" + Quote(value);
        }

        internal static string Quote(string s)
        {
            return s == null ? "\"\"" : Regex.IsMatch(s, "\"") ? Regex.IsMatch(s, "'") ? Regex.Replace(s, "\"", "&qout;") : ("'" + s + "'") : ("\"" + s + "\"");
        }
	}
}
